package com.lntea.mybatisDemo.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.lntea.mybatisDemo.domain.CmCorporation;
import com.lntea.mybatisDemo.mapper.CmCorporationMapper;

public class CmCorporationService {
	private SqlSessionFactory sqlSessionFactory;

	@Before
	public void beforeQuery() {
		String resource = "mybatis-config.xml";
		try {
			InputStream is = Resources.getResourceAsStream(resource);

			/*
			 * InputStream is2 =
			 * this.getClass().getClassLoader().getResourceAsStream(
			 * "conf2.properties"); Properties properties = new Properties();
			 * properties.load(is2); is2.close();
			 */

			sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	// 根据商户号查询商户
	public void queryAllCmCorporation() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			CmCorporationMapper mapper = session.getMapper(CmCorporationMapper.class);
			CmCorporation cm = mapper.queryAllCmCorporation("102002858120004");
			/*
			 * List<CmCorporation> cmList = session .selectList(
			 * "com.lntea.mybatisDemo.mapper.CmCorporationMapper.queryAllCmCorporation"
			 * );
			 */
			System.out.println(cm.getLegalPersonName());
		} finally {
			session.close();
		}
	}

	@Test
	// 根据商户号查询商户账户
	public void queryMerchantAccount() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			CmCorporation cm = session.selectOne("com.lntea.mybatisDemo.mapper.CmCorporationMapper.findMerchantAccount",
					"106002750452792");
			System.out.println(cm.getAccountNo());
		} finally {
			session.close();
		}
	}

	@Test
	// 根据商户号查询商户结算类型
	public void findSettleTypeByMerchantNo() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			CmCorporation cm = session.selectOne("com.lntea.mybatisDemo.mapper.CmCorporationMapper.findSettleTypeByMerchantNo",
					"106002750452792");
			System.out.println(cm.getSettleType());
		} finally {
			session.close();
		}
	}
	
	@Test
	//根据商户号和给定金额查询商户是否有大于给定金额的结算
	public void querySettleBatch(){
		LogFactory.useLog4JLogging();
		SqlSession session = sqlSessionFactory.openSession();
		try {
			CmCorporation cm = new CmCorporation();
			cm.setMerchantNo("106002750452792");
			cm.setThanValue("10000");
			int result = session.selectOne("com.lntea.mybatisDemo.mapper.CmCorporationMapper.querySettleBatch",
					cm);
			System.out.println(result);
		} finally {
			session.close();
		}
	}
	
	
}
